home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / extra / expand_args.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  5KB  |  236 lines

  1.  
  2. /*
  3.  *  EXPAND_ARGS.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <libraries/dos.h>
  12. #include <clib/dos_protos.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <lib/misc.h>
  17.  
  18. #define MAXPATHLEN  512
  19.  
  20. static char **MyAv;
  21. static int   AcIdx;
  22. static int   AcMax;
  23.  
  24. typedef struct FileInfoBlock FIB;
  25.  
  26. int ExpandElements(char *, short, char *, char *);
  27. int AddArgument(char *);
  28. int expand_args(int, const char **, int *, char ***);
  29.  
  30. int
  31. expand_args(ac, av, nac, nav)
  32. int   ac;
  33. const char **av;
  34. int   *nac;
  35. char ***nav;
  36. {
  37.     char *tmp1 = malloc(MAXPATHLEN);        /*  max path len    */
  38.     char *tmp2 = malloc(MAXPATHLEN);        /*  max path len    */
  39.     char *arg;
  40.     int error = 0;
  41.  
  42.     MyAv  = malloc(sizeof(char *) * 8);
  43.     AcIdx = 1;
  44.     AcMax = 8;
  45.     *nac = 0;
  46.  
  47.     if (MyAv == NULL || tmp1 == NULL || tmp2 == NULL)
  48.     return(-1);
  49.  
  50.     MyAv[0] = *av;
  51.  
  52.     while (error >= 0 && (arg = *++av)) {
  53.     short i;
  54.     char c;
  55.  
  56.     for (i = 0; c = arg[i]; ++i) {
  57.         if (c == '#' || c == '?' || c == '|')
  58.         break;
  59.     }
  60.     if (c == 0) {
  61.         AddArgument(arg);
  62.         continue;
  63.     }
  64.  
  65.     tmp1[0] = 0;    /*  clear path buf  */
  66.  
  67.  
  68.     error = ExpandElements(tmp1, 0, arg, tmp2);
  69.     }
  70.     if (error == 0) {
  71.     MyAv[AcIdx] = NULL;
  72.     *nac = AcIdx;
  73.     *nav = MyAv;
  74.     }
  75.     return(error);
  76. }
  77.  
  78. /*
  79.  *  ExpandElements(tmp1, t1len, wild, tmp2)
  80.  *    tmp1    pointer to constructed path so far (is the current directory)
  81.  *    t1len    index to end of constructed path
  82.  *    wild    wildcard argument path remainder
  83.  *    tmp2    scratch buf
  84.  */
  85.  
  86. static int
  87. ExpandElements(tmp1, t1len, wild, tmp2)
  88. char *tmp1;
  89. short t1len;
  90. char *wild;
  91. char *tmp2;
  92. {
  93.     BPTR oldLock;
  94.     BPTR lock;
  95.     short argOk = 1;
  96.     int error = 0;
  97.  
  98.     {
  99.     lock = Lock("", SHARED_LOCK);
  100.  
  101.     if (lock == NULL)
  102.         return(-1);
  103.     oldLock = CurrentDir(lock);
  104.     }
  105.  
  106.     while (*wild) {
  107.     char c;
  108.     short i;
  109.     short fndwild = 0;
  110.  
  111.     for (i = 0; c = wild[i]; ++i) {
  112.         switch(c) {
  113.         case '/':
  114.         case ':':
  115.         case 0:
  116.         break;
  117.         case '#':
  118.         case '?':
  119.         case '(':
  120.         case '|':
  121.         fndwild = 1;
  122.         default:
  123.         continue;
  124.         }
  125.         break;
  126.     }
  127.     strncpy(tmp2, wild, i);
  128.     tmp2[i] = 0;
  129.     if (wild[i] == ':')
  130.         strcat(tmp2, ":");
  131.     if (i == 0 && wild[i] == '/')
  132.         strcat(tmp2, "/");
  133.  
  134.     /*printf("fndw %d tmp1=%s tmp2=%s i = %d wild = %s\n", fndwild, tmp1, tmp2, i, wild);*/
  135.  
  136.     if (fndwild == 0) {
  137.         lock = Lock(tmp2, SHARED_LOCK);
  138.         if (lock == NULL) {         /*  path element failed */
  139.         argOk = 0;
  140.         break;
  141.         }
  142.         if (wild[i] == '/') {       /*  path element?       */
  143.         FIB *fib;
  144.  
  145.         if (i)
  146.             strcat(tmp2, "/");
  147.         if (fib = malloc(sizeof(FIB))) {
  148.             if (Examine(lock, fib) == 0 || fib->fib_DirEntryType < 0) {
  149.             argOk = 0;
  150.             free(fib);
  151.             UnLock(lock);
  152.             break;
  153.             }
  154.             free(fib);
  155.         }
  156.         }
  157.  
  158.         UnLock(CurrentDir(lock));   /*  success, append     */
  159.  
  160.         strcat(tmp1, tmp2);
  161.         t1len = strlen(tmp1);
  162.  
  163.         wild += i;
  164.         if (*wild == ':' || *wild == '/')
  165.         ++wild;
  166.     } else {
  167.         FIB *fib;
  168.         void *wildnode;
  169.         short addslash = 0;
  170.  
  171.         _SetWildStack(2048);
  172.         wild += i;
  173.         if (wild[0] == '/') {
  174.         ++wild;
  175.         addslash = 1;
  176.         }
  177.  
  178.         if (wildnode = _ParseWild(tmp2, strlen(tmp2))) {
  179.         if (fib = malloc(sizeof(FIB))) {
  180.             if (Examine(lock, fib) && fib->fib_DirEntryType > 0) {
  181.             while (error >= 0 && ExNext(lock, fib)) {
  182.                 long xlock;
  183.  
  184.                 /*printf("FIB: %s\n", fib->fib_FileName);*/
  185.                 if (_CompWild(fib->fib_FileName, wildnode, NULL) >= 0) {
  186.                 if (xlock = Lock(fib->fib_FileName, SHARED_LOCK)) {
  187.                     xlock = CurrentDir(xlock);
  188.                     strcpy(tmp1 + t1len, fib->fib_FileName);
  189.                     if (addslash)
  190.                     strcat(tmp1 + t1len, "/");
  191.                     /*printf("ADDSL %d, wild %s\n", addslash, wild);*/
  192.                     if (ExpandElements(tmp1, t1len + addslash + strlen(fib->fib_FileName), wild, tmp2 + strlen(tmp2) + 1) < 0)
  193.                     error = -1;
  194.                     UnLock(CurrentDir(xlock));
  195.                 }
  196.                 } else {
  197.                 ;
  198.                 }
  199.             }
  200.             }
  201.             free(fib);
  202.         }
  203.         _FreeWild(wildnode);
  204.         }
  205.         argOk = 0;
  206.         break;
  207.     }
  208.     }
  209.  
  210.     /*
  211.      *    'lock' variable could be illegal here
  212.      */
  213.  
  214.     if (argOk) {
  215.     if (AddArgument(tmp1) < 0)
  216.         error = -1;
  217.     }
  218.     UnLock(CurrentDir(oldLock));
  219.     return(error);
  220. }
  221.  
  222. static int
  223. AddArgument(arg)
  224. char *arg;
  225. {
  226.     MyAv[AcIdx] = strdup(arg);
  227.     if (++AcIdx == AcMax) {
  228.     AcMax *= 2;
  229.     MyAv = realloc(MyAv, sizeof(char *) * AcMax);
  230.     if (MyAv == NULL)
  231.         return(-1);
  232.     }
  233.     return(0);
  234. }
  235.  
  236.